const info = require("../../../js/modules/info.js");
import {
  detail,
  mall,
  list,
  scant
} from '../../../js/modules/mall';
import {
  activities
} from '../../../js/modules/market';
import {
  api,
  util,
  dialog,
  forms,
  storage
} from '../../../js/util';
import {
  router
} from '../../../js/router';
import authorize from '../../../js/modules/authorize'
var app = getApp();
const componentBehavior = require('../../../js/common/ComponentBehavior')

Component({
  behaviors: [componentBehavior],
  lifetimes: {
    attached() {
      // 在组件实例进入页面节点树时执行
    },
    detached() {
      // 在组件实例被从页面节点树移除时执行
    },
  },
  /**
   * 组件的属性列表
   */
  properties: {
    scantyProduct:{
      value:{},
      type:Object
    },
    Currency:String,
  },

  /**
   * 组件的初始数据
   */
  data: {
    imgDomain: api.imgDomain,
    enableOss: api.enableOss,
    PicVerifyCode:'',
    ScantyMobile: api.ScantyMobile,
    quantityDigits:2,
  },

  /**
   * 组件的方法列表
   */
  methods: {
    /*
    弹出缺货登记的层 
    */
    openScanyDialog(e) {
      var that = this;
      if (authorize.isLogin('store')&&!api.ScantyMobile) {
        //获得默认手机号
        this.scantyPhone();
      }
      this.setData({PicVerifyCode: ''}, () =>{
        dialog.showModal(that, 'showModalStatus')
      });

      //设置回调
      // this.PicVerifyCode = this.selectComponent("#PicVerifyCode");
      // this.PicVerifyCode.data.callback = info.getPicVerifyCode;
      //调用一次图片验证码
      // this.PicVerifyCode.bindPicVerifyCode();
    },
    /**隐藏弹出层 */
    hideModal() {
      var that = this;
      dialog.hideModal(that, 'showModalStatus')
    },
    /**
     * 表单值绑定
     */
    bindFormValue: function (e) {
      e = e.detail;
      this.data[e.target.id] = e.detail.value;

    },
    /**
     * 提交缺货登记 
     * 
     * */
    onSubmitScanty() {
      //验证手机号
      var mobile = this.data.ScantyMobile;
      if(mobile){
        var mobileItem = {
          value: util.trim(mobile),
          validate: "isnull|mobile",
          error: "请输入正确的手机号"
        }
        if (!forms.validate(mobileItem)) return false;
      }
      //验证验证码 
      // var picvalidatecode = this.data.PicVerifyCode;
      // if (picvalidatecode.length == 0) {
      //   util.alert("请先输入4位验证码");
      //   return;
      // }
      // var codeToken = storage.get("pic_verify_code");
      // if (codeToken == undefined || codeToken.length == 0) {
      //   util.alert("图片验证码过期");
      //   return;
      // }
      var scantyProduct = this.data.scantyProduct;
      var param = {
        ProductId: scantyProduct.ProductId,
        GoodsId: scantyProduct.ProductGoodsId,
        ProductName: scantyProduct.ProductName,
        BrandId: scantyProduct.BrandId,
        Mobile: mobile,
        Quantity:scantyProduct.CartCount,
        Unit:scantyProduct.AuxUnit || scantyProduct.DefaultUnit || scantyProduct.Unit
        // VerifyCode: picvalidatecode,
        // CodeToken: codeToken
      }
      var that = this;
      scant.saveScanty(param, function (r) {

        if(r.Success){

          util.alert(r.Message, "温馨提示", function () {

            that.hideModal();
            that.setData({
              picVerifyCode:''
            })
          });
        }
        else if (r.Code <0){
          util.tips(r.Message);
        }
        else{
          util.tips(r.Message);
          that.hideModal();
          that.setData({
            picVerifyCode:''
          })
        }
        
      });
    },
    preventTouchMove(){
      
    },
    /**
     * 加入完成后调用，更新界面显示数量
     */
    onAddCartCompleted(e) {
      let product = this.data.scantyProduct;
      var num = e.detail.value;
      product.CartCount = num;
      this.setData({
        scantyProduct: product
      });
    },
    /**
   * 改变单位
   */
    onChangeGoodsUnitEvent: function (e) {
      let that = this;
      let g = that.data.scantyProduct;
      let {idx} = e.currentTarget.dataset;
      let unit = g.AuxiliaryUnits[idx];
      g.Translated = unit.Translated;
      g.AuxUnit = unit.Name;
      g.ZIndex = idx;
      if(g.isPanic&&g.PanicGoods){
        g.PanicGoods.map(function(p){
          if (g.GoodsId == p.GoodsId) {
            var t = detail.parseRule(p.UnitPrices, g.Translated, 0, false);
            if (t.has) {
              g.SalesPrice = t.price;
              if(t.integral){
                g.SalesIntegral = t.integral;
              }
            }
          }
        })
      }else{
        var t = detail.parseRule(g.UnitPrices, g.Translated, 0, false);
        if (t.has) {
          g.SalesPrice = t.price;
          if(t.integral){
            g.SalesIntegral = t.integral;
          }
        }
      }
      
      that.setData({
        scantyProduct:g
      })
    },
     /**
     * 获取缺货登记手机号码
     */
    scantyPhone(){
      var that =this;
      scant.getScantyPhone(function (res) {
        api.ScantyMobile = res.Content;
        that.setData({
          ScantyMobile: res.Content
        })
      });
    },
  }
})
